package body murs is



   function get_post(posts:access les_post;I:Natural) return access un_post is
   begin
	return posts.all(I);
   end get_post;


   function est_defini(un_mur: mur; I: Natural) return boolean is
      BorneInf : natural := un_mur.nextPost.all'First;
      BorneSup : natural := un_mur.nextPost.all'Last;
      Milieu : natural := (BorneSup + BorneInf) / 2 ;
   begin
      loop
         Milieu := (BorneSup + BorneInf) / 2 ;
         IF BorneInf <= BorneSup THEN
            if un_mur.nextPost.all(milieu).all.N = I then
               return true;
            else
               IF I <= un_mur.nextPost.all(milieu).all.N then
                  BorneSup := Milieu -1;
               ELSE
                  BorneInf := Milieu + 1;
               END IF;
            end if ;

         ELSE
            return false;
         END IF;
      end loop;
   end est_defini;

   function getIndice(un_mur: mur; I: Natural) return natural is
      BorneInf : natural := un_mur.nextPost.all'First;
      BorneSup : natural := un_mur.nextPost.all'Last;
      Milieu : natural := (BorneSup + BorneInf) / 2 ;
   begin
      loop
         Milieu := (BorneSup + BorneInf) / 2 ;
         IF BorneInf <= BorneSup THEN
            if un_mur.nextPost.all(milieu).all.N = I then
               return milieu;
            else
               IF I <= un_mur.nextPost.all(milieu).all.N then
                  BorneSup := Milieu -1;
               ELSE
                  BorneInf := Milieu + 1;
               END IF;
            end if ;

         ELSE
            return milieu;
         END IF;
      end loop;
   end getIndice;

   function ajouterCommentaire (un_mur:mur; T:message; Auteur:Individu;I:Natural) return mur is
      M2 : access mur := new mur'(un_mur);
      indice : natural := getIndice(M2.all,I);
      le_com_tableau : les_com := (1=> new un_commentaire'(M => new message'(T),
                                      I => new individu'(Auteur)));
   begin
      M2.all.nextPost.all(indice).all.nextCom :=
        new les_com'(
          M2.all.nextPost.all(indice).all.nextCom.all(
            M2.all.nextPost.all(indice).all.nextCom.all'First ..
              M2.all.nextPost.all(indice).all.nextCom.all'Last)
         &le_com_tableau);
   return M2.all;
   end ajouterCommentaire;

   function ajouterPost (un_mur:mur; T:message; Auteur:Individu;I:natural) return mur is

      les_coms : access les_com := new les_com(1..0);

      le_post_tableau : les_post := (1 => new un_post'(M       => new Message'(T),
                                                       I       => new individu'(Auteur),
                                                       N       => I,
                                                       nextCom => new les_com'(les_coms.all)));

      M2 : access mur := new mur'(un_mur);
   begin
      M2.all.nextPost := new les_post'(
          M2.all.nextPost.all(M2.all.nextPost.all'First .. M2.all.nextPost.all'Last) & le_post_tableau);
      return M2.all;
   end ajouterPost;

   function ajouter(un_mur:mur; T:message; Auteur:Individu;I:natural) return mur is
   begin
      if est_defini(un_mur,I) then
         return ajouterCommentaire(un_mur,T,Auteur,I);
      else
         return ajouterPost(un_mur,T,Auteur,I);
      end if;
   end ajouter;


   function auteur(un_mur : mur ; N : Natural) return individu is
   begin
      return get_post(un_mur.nextPost,N).all.I.all;
   end auteur;


   function creer (Proprietaire : individu ) return mur is
      le_post_tableau : access les_post := new les_post(1..0);
   begin
      return (Proprietaire => new individu'(Proprietaire),
              NextPost => new les_post'(le_post_tableau.all));
   end creer;

   function getMur(un_mur : Mur) return String is
      S : access string := new String(1..0);

      function getCom (les_coms : access les_com) return string is
         S : access string := new String(1..0);
      begin
         for i in les_coms.all'Range loop
            S := new String'(S.all
                             & ASCII.LF
                             & ASCII.HT
                             & putIndividu(les_coms.all(i).all.I.all)
                             & " (C): "
                             & putmessage(les_coms.all(i).all.M.all));
         end loop;
         return S.all;
      end getCom;
   begin
      for i in un_mur.nextPost.all'Range loop
         S:= new String'(
                         S.all
                         & ASCII.LF
                         & putIndividu(un_mur.nextPost.all(i).all.I.all)
                         & " : (" & Natural'Image(un_mur.nextPost.all(i).N) & ") : "
                         & putMessage(un_mur.nextPost.all(i).all.M.all)
                         & getCom(un_mur.nextPost.all(i).all.nextCom));
      end loop;
      return S.all;

   end getMur;
end murs;
